package server.console;

import java.io.Console;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * This class will allow the server administrator to issue commands from the
 * command line. TODO: Complete this code.
 *
 * @author Vincent Tsuei
 */
public class ConsoleHandler implements Runnable {

    private static ConsoleHandler instance = new ConsoleHandler();
    private static Console console = System.console();
    private static boolean runConsole = true;

    private ConsoleHandler() {
    }

    public static boolean ConsoleAvail() {
        return instance != null;
    }

    @Override
    public void run() {
        while (runConsole) {
            synchronized (this) {
                try {
                    wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(ConsoleHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void RunConsole() {
        if (ConsoleAvail()) {
            instance.run();
        } else {
            throw new RuntimeException("Console not available!");
        }
    }

    public static void ShutdownConsoleHandler() {
        runConsole = false;
    }
}
